package ai.people.netmon.telegram.service.impl;

import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.domain.searchmsg.request.MessageRequest;
import ai.people.netmon.framework.domain.telegram.entity.AnalysisDataUser;
import ai.people.netmon.framework.domain.telegram.entity.Channels;
import ai.people.netmon.framework.domain.telegram.vo.channels.AnalyzedChannels;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.domain.telegram.vo.channels.SimilarChannels;
import ai.people.netmon.framework.domain.telegram.vo.channels.UserMember;
import ai.people.netmon.framework.exception.enums.GroupExceptionEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.channels.AccountMemberRequest;
import ai.people.netmon.telegram.domain.request.channels.ChartMessageRequest;
import ai.people.netmon.telegram.domain.request.channels.KeyGoalsRequest;
import ai.people.netmon.telegram.domain.request.channels.SimilarChannelsRequest;
import ai.people.netmon.telegram.mapper.ChannelsAnalysisServiceMapper;
import ai.people.netmon.telegram.service.AccountAnalysisService;
import ai.people.netmon.telegram.service.AnalysisDataUserService;
import ai.people.netmon.telegram.service.ChannelsAnalysisService;
import ai.people.netmon.telegram.service.MessagesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 群组分析服务
 *
 * @author yuanqinglong
 * @date 2022/1/11 20:23
 */
@Slf4j
@Service
public class ChannelsAnalysisServiceImpl implements ChannelsAnalysisService {


    @Autowired
    private ChannelsAnalysisServiceMapper channelsAnalysisServiceMapper;

    @Autowired
    private AnalysisDataUserService analysisDataUserServiceImpl;

    @Autowired
    private AccountAnalysisService accountAnalysisServiceImpl;

    @Autowired
    private MessagesService messagesServiceImpl;

    /**
     * 获取群组信息
     *
     * @param id id
     * @return {@link Channels}
     */
    @Override
    public Channels getChannels(Long id) {
        Channels channels = channelsAnalysisServiceMapper.selectById(id);
        AssertUtils.isNotNull(channels, GroupExceptionEnum.GROUP_DOES_NOT_EXIST);
        return channels;
    }


    /**
     * 加入分析
     *
     * @param analysisDataUser 分析数据用户
     */
    @Override
    public void joinAnalysis(AnalysisDataUser analysisDataUser) {
        // TODO: 2022/1/18 判断加入分析数是否大于配置的分析数


        analysisDataUserServiceImpl.joinAnalysis(analysisDataUser);
    }

    /**
     * 成员列表
     *
     * @param accountMemberRequest 群组成员参数
     * @return {@link PageResponse}<{@link UserMember}>
     */
    @Override
    public PageResponse<UserMember> memberList(AccountMemberRequest accountMemberRequest) {
        long memberCount = channelsAnalysisServiceMapper.memberCount(accountMemberRequest);
        if (memberCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<UserMember> memberList = channelsAnalysisServiceMapper.memberList(accountMemberRequest);
        // TODO: 2022/1/12 处理加入分析标识  tags字段
        //AnalyzedListRequest analyzedListRequest = new AnalyzedListRequest(null, AnalysisTypeEnum.CHANNELS.getCode(), accountMemberRequest.getAnalysisSource());
        //Set<String> analyzedAccountIdSet = accountAnalysisServiceImpl.analyzedAccountIdSet(analyzedListRequest);
        //memberList.forEach(userMember -> userMember.setJoinStatus(analyzedAccountIdSet.contains(String.valueOf(userMember.getId()))));
        return PageResponse.builder(memberList, memberCount);
    }


    /**
     * 关键目标
     *
     * @param keyGoalsParameter 关键目标参数
     * @return {@link Object}
     */
    @Override
    public Object keyGoals(KeyGoalsRequest keyGoalsParameter) {
        return null;
    }

    /**
     * 聊天信息
     *
     * @param chartMessageRequest 聊天信息参数
     * @return {@link PageResponse}<{@link ChatMessage}>
     */
    @Override
    public PageResponse<ChatMessage> chatMessage(ChartMessageRequest chartMessageRequest) {
        MessageRequest messageQuery = chartMessageRequest.createMessageQuery(AnalysisSourceEnum.telegram.name());
        return messagesServiceImpl.globalInfoSearch(messageQuery);
    }

    /**
     * 同类群组
     *
     * @param similarChannelsRequest 同类群组参数
     * @return {@link List}<{@link SimilarChannels}>
     */
    @Override
    public List<SimilarChannels> similarChannels(SimilarChannelsRequest similarChannelsRequest) {
        return channelsAnalysisServiceMapper.similarChannels(similarChannelsRequest);
    }


    /**
     * 已添加分析群组
     *
     * @param analyzedListRequest 参数
     * @return {@link List}<{@link AnalyzedChannels}>
     */
    @Override
    public List<AnalyzedChannels> analyzedChannels(AnalyzedListRequest analyzedListRequest) {
        return channelsAnalysisServiceMapper.analyzedChannels(analyzedListRequest);
    }

    /**
     * 已添加分析群组id
     *
     * @param analyzedListRequest 分析列表请求
     * @return {@link Set}<{@link String}>
     */
    @Override
    public Set<String> analyzedChannelsIdSet(AnalyzedListRequest analyzedListRequest) {
        return channelsAnalysisServiceMapper.analyzedChannelsIdSet(analyzedListRequest);
    }

    /**
     * 添加群组
     *
     * @param urlOrId url
     */
    @Override
    public void addChannels(String urlOrId) {
        LambdaQueryWrapper<Channels> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.select(Channels::getGroupid);
        lambdaQuery.eq(Channels::getGroupurl, urlOrId).or().eq(Channels::getGroupid, urlOrId);
        Channels selectOne = channelsAnalysisServiceMapper.selectOne(lambdaQuery);
        AssertUtils.isNotNull(selectOne,GroupExceptionEnum.GROUP_DOES_NOT_EXIST);
        String groupId = selectOne.getGroupid();
        // TODO: 2022/1/18 调用加入分析接口
        AnalysisDataUser analysisDataUser = new AnalysisDataUser();
        // 加入分析
        this.joinAnalysis(analysisDataUser);
    }
}
